﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Easy.Common
{
    public enum CustomExceptionState
    {
        SUCCESS = 0, //"成功"),
        NoReadPrivilege = 1, //"没有读权限"),
        NoUpdatePrivilege = 2, //"没有写权限"),
        NoCreatePrivilege = 32, //"没有创建权限"),
        NoDeletePrivilege = 65536, //"没有删除权限"),
        NoAssignPrivilege = 524288, //"没有分派权限")
        InputParameterError = 1000, //参数传入错误
        CreateForbidedByBeforeCreatePlugin=10240,//创建被插件阻止
        BeforeCreatePluginCrash = 10241,//创建前插件崩溃
        AfterCreatePluginCrash=10242,
        UpdateForbidedByBeforePlugin=10243,
        BeforeUpdatePluginCrash=10244,
        AfterUpdatePluginCrash=10245,
        DeleteForbidedByBeforeDeletePlugin=10246,
        BeforeDeletePluginCrash=10247,
        AfterDeletePluginCrash=10248,
        AssignForbidedByBeforeAsignPlugin=10249,
        BeforeAssignPluginCrash=10250,
        AfterAssignPluginCrash=10251,
        Other = -1
    }
    public class CustomException : Exception
    {
        private static readonly Dictionary<CustomExceptionState, string> StateMessage = new Dictionary<CustomExceptionState, string>
        {
          {CustomExceptionState.SUCCESS,"成功"},
          {CustomExceptionState.NoReadPrivilege,"没有读权限"},
          {CustomExceptionState.NoUpdatePrivilege,"没有写权限"},
          {CustomExceptionState.NoCreatePrivilege,"没有创建权限"},
          {CustomExceptionState.NoDeletePrivilege,"没有删除权限"},
          {CustomExceptionState.NoAssignPrivilege,"没有分派权限"},
          {CustomExceptionState.InputParameterError,"参数传入错误"},
          {CustomExceptionState.CreateForbidedByBeforeCreatePlugin, "创建操作被Plugin阻止了更新"},
          {CustomExceptionState.DeleteForbidedByBeforeDeletePlugin, "删除操作被Plugin阻止了更新"},
          {CustomExceptionState.UpdateForbidedByBeforePlugin, "更新操作被Plugin阻止了更新"},
          {CustomExceptionState.AssignForbidedByBeforeAsignPlugin,"分派被Plugin阻止"},
          {CustomExceptionState.BeforeCreatePluginCrash, "创建前Plugin崩溃"},
          {CustomExceptionState.AfterCreatePluginCrash, "创建后Plugin崩溃"},
          {CustomExceptionState.BeforeDeletePluginCrash, "删除前Plugin崩溃"},
          {CustomExceptionState.AfterDeletePluginCrash, "删除后Plugin崩溃"},
          {CustomExceptionState.BeforeUpdatePluginCrash, "更新前Plugin崩溃"},
          {CustomExceptionState.AfterUpdatePluginCrash, "更新后Plugin崩溃"},
          {CustomExceptionState.BeforeAssignPluginCrash, "分派前Plugin崩溃"},
          {CustomExceptionState.AfterAssignPluginCrash, "分派后Plugin崩溃"},
          {CustomExceptionState.Other,"未定义错误消息"}
        };

        private CustomResult cusResult;
        public CustomResult CustomResult
        {
            get { return cusResult; }
        }
        public CustomException(CustomExceptionState state, string ExtMsg)
            : base(StateMessage[state])
        {
            this.cusResult = new CustomResult()
            {
                ResultCode = (int)state,
                ResultMessage = StateMessage[state],
                ResultData = ExtMsg
            };
        }
    }

    public class CustomResult
    {
        public int ResultCode { get; set; }//状态码
        public String ResultMessage { get; set; } //系统信息
        public object ResultData { get; set; } //返回的数据
    }
}
